package com.lzw.service.impl.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lzw.mapper.admin.DormitoryAdminManagementMapper;
import com.lzw.pojo.DormitoryAdmin;
import com.lzw.service.admin.DormitoryAdminManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DormitoryAdminManagementServiceImpl implements DormitoryAdminManagementService {

    @Autowired
    private DormitoryAdminManagementMapper dormitoryAdminManagementMapper;

    @Override
    public List<DormitoryAdmin> listDormitoryAdmins(Integer pageNum, Integer pageSize, String name, String gender, Integer dormitoryBuildingId) {
        PageHelper.startPage(pageNum,pageSize);
        List<DormitoryAdmin> list = dormitoryAdminManagementMapper.listDormitoryAdmins(name, gender, dormitoryBuildingId);
        Page<DormitoryAdmin> dormitoryAdmins = (Page<DormitoryAdmin>) list;
        return dormitoryAdmins;
    }

    @Override
    public void addDormitoryAdmin(DormitoryAdmin dormitoryAdmin) {
        if (dormitoryAdminManagementMapper.countByName(dormitoryAdmin.getName()) > 0){
            throw new IllegalArgumentException("公寓管理员已存在");
        }
        if(dormitoryAdminManagementMapper.countByUsername(dormitoryAdmin.getUsername()) > 0){
            throw new IllegalArgumentException("公寓管理员用户名已存在");
        }
        if (dormitoryAdminManagementMapper.countByPhone(dormitoryAdmin.getPhone()) > 0){
            throw new IllegalArgumentException("公寓管理员电话已存在");
        }
        dormitoryAdminManagementMapper.addDormitoryAdmin(dormitoryAdmin);
    }

    @Override
    public void deleteDormitoryAdmin(List<Integer> ids) {
        dormitoryAdminManagementMapper.deleteDormitoryAdmin(ids);
    }

    @Override
    public void updateDormitoryAdmin(DormitoryAdmin dormitoryAdmin) {
        DormitoryAdmin existing = dormitoryAdminManagementMapper.getDormitoryAdminById(dormitoryAdmin.getAdminId());

        //公寓管理员是否存在
        if (existing == null){
            throw new IllegalArgumentException("公寓管理员不存在");
        }

        //公寓管理员姓名是否重复
        if (!existing.getName().equals(dormitoryAdmin.getName())
                && dormitoryAdminManagementMapper.countByName(dormitoryAdmin.getName()) > 0){
            throw new IllegalArgumentException("公寓管理员姓名已存在");
        }

        //公寓管理员用户是否重复
        if (!existing.getUsername().equals(dormitoryAdmin.getUsername())
                && dormitoryAdminManagementMapper.countByUsername(dormitoryAdmin.getUsername()) > 0){
            throw new IllegalArgumentException("公寓管理员用户名已存在");
        }

        //公寓管理员电话是否重复
        if (!existing.getPhone().equals(dormitoryAdmin.getPhone())
                && dormitoryAdminManagementMapper.countByPhone(dormitoryAdmin.getPhone()) > 0){
            throw new IllegalArgumentException("公寓管理员电话已存在");
        }
        dormitoryAdminManagementMapper.updateDormitoryAdmin(dormitoryAdmin);
    }

    @Override
    public DormitoryAdmin getDormitoryAdminById(int id) {
        return dormitoryAdminManagementMapper.getDormitoryAdminById(id);
    }

    @Override
    public List<DormitoryAdmin> listAll() {
        return dormitoryAdminManagementMapper.listAll();
    }
}
